package com.deer.wms.inventory.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deer.wms.base.system.service.CellInfoService;
import com.deer.wms.inventory.constant.InventoryConstant;
import com.deer.wms.inventory.dao.InventoryMapper;
import com.deer.wms.inventory.dao.InventoryTransactMapper;
import com.deer.wms.inventory.model.Inventory.Inventory;
import com.deer.wms.inventory.model.Inventory.InventoryTransact;
import com.deer.wms.inventory.model.Inventory.InventoryTransactCriteria;
import com.deer.wms.inventory.model.Inventory.InventoryTransactDto;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.inventory.service.InventoryTransactService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.system.manage.model.organization.Organization;
import com.deer.wms.system.manage.service.OrganizationService;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by 郭靖勋 on 2019/12/18.
 */
@Service
@Transactional
public class InventoryTransactServiceImpl extends ServiceImpl<InventoryTransactMapper, InventoryTransact> implements InventoryTransactService {

    @Autowired
    private InventoryTransactMapper inventoryTransactMapper;
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private CellInfoService cellInfoService;


    @Value("${wave.direct}")
    //直发虚仓
    private Integer directId;
    @Value("${wave.vmi}")
    //VMI仓库id
    private Integer vmiId;



    @Override
    public List<InventoryTransactDto> findList(InventoryTransactCriteria criteria) {
        return inventoryTransactMapper.findList(criteria);
    }

    @Override
    public int run(InventoryTransact inventoryTransact) {
        Integer transactType = inventoryTransact.getTransactType();

        //TYPE: 到货 / 验收
        if (transactType.equals(InventoryConstant.TRANSACT_TYPE_ACCEPT)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_BLIND) ||
                transactType.equals(InventoryConstant.TRANSACT_TYPE_ASN)) {
            Inventory inventory = handleInStore(inventoryTransact);
            return inventory.getInventoryId();
        }

        //TYPE: 装箱
        if (InventoryConstant.TRANSACT_TYPE_PACK.equals(transactType)) {
            Inventory inventory = handlePack(inventoryTransact);
            return inventory.getInventoryId();
        }

        //TYPE: 上架 或 转移 或整箱拣货
        if (InventoryConstant.TRANSACT_TYPE_UP.equals(transactType) || InventoryConstant.TRANSACT_TYPE_MOVE.equals(transactType) || InventoryConstant.TRANSACT_TYPE_PICK.equals(transactType)) {
            Inventory inventory = handleTransfer(inventoryTransact);
            return inventory.getInventoryId();
        }

        //TYPE: 分拣
        if (InventoryConstant.TRANSACT_TYPE_PICK2.equals(transactType)) {
            Inventory inventory = handlePick(inventoryTransact);
            return inventory.getInventoryId();
        }

        //TYPE: 出库 不需要这一块逻辑
        if (InventoryConstant.INVENTORY_TYPE_OUT.equals(transactType)) {
            Inventory inventory = handleOutStore(inventoryTransact);
            return inventory.getInventoryId();
        }

        //TYPE: 库存调整
        if (transactType.equals(InventoryConstant.INVENTORY_ADJUST)) {
            Inventory inventory = handleAdjust(inventoryTransact);
            return inventory.getInventoryId();
        }

        //TYPE: 解冻
        if (transactType.equals(InventoryConstant.INVENTORY_UNFREEZE) || transactType.equals(InventoryConstant.INVENTORY_FREEZE)) {
            //按照条件找到所有的库存信息
            QueryWrapper<Inventory> qw = Wrappers.query();
            Map<String, Object> map = new HashMap<>(3);
            //根据物料批次找到所有对应的库存
            map.put("ware_id", inventoryTransact.getWareId());
            map.put("item_code", inventoryTransact.getItemCode());
            map.put("batch_name", inventoryTransact.getFromBatchName());
            List<Inventory> inventories = inventoryMapper.selectList(qw.allEq(map, true));
            for (Inventory inventory : inventories) {
//                inventory.setErpAreaId();
//                inventory.setErpItemId();
                if (transactType.equals(InventoryConstant.INVENTORY_UNFREEZE)) {
                    inventory.setState(InventoryConstant.TRANSACT_STATE_NORMAL);
                    inventory.setFreezeQuantity(0.0);
                    inventory.setStateDis(null);
                } else {
                    inventory.setState(InventoryConstant.TRANSACT_STATE_UNNORMAL);
                    inventory.setStateDis(inventoryTransact.getStateDis());
                }

            }
            inventoryService.saveOrUpdateBatch(inventories);
        }
        return 0;
    }

    /**
     * 分拣类型事务提交
     *
     * @param inventoryTransact 事务
     * @return Inventory
     */
    private Inventory handlePick(InventoryTransact inventoryTransact) {

        Inventory fromInventory = inventoryService.getById(inventoryTransact.getToInventoryId());

        //来源库存扣减
        if (ObjectUtil.isNotNull(fromInventory)) {
            //扣减总数量，扣减分配数量 更新
            fromInventory.setQuantity(fromInventory.getQuantity() - inventoryTransact.getFromQuantity());
            fromInventory.setAllotQuantity(fromInventory.getAllotQuantity() - inventoryTransact.getFromQuantity());
            fromInventory.setAllotTransRatio(1.0);
            inventoryService.updateById(fromInventory);
        } else {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "未找到对应库存");
        }

        //目的库存
        Inventory toInventory;
        toInventory = processInventoryData(inventoryTransact);
        toInventory.setMtoNo(fromInventory.getMtoNo());
        toInventory.setPackDescribe(fromInventory.getPackDescribe());
        toInventory.setErpAreaId(fromInventory.getErpAreaId());
        toInventory.setErpItemId(fromInventory.getErpItemId());
        inventoryService.save(toInventory);


        /**
         * 如果库存为0删除原单
         */
        if (fromInventory.getQuantity() <= 0) {
            inventoryService.removeById(fromInventory.getInventoryId());
        }

        return toInventory;
    }

    /**
     * 出库类型事务提交
     *
     * @param inventoryTransact 事务
     * @return Inventory
     */
    private Inventory handleOutStore(InventoryTransact inventoryTransact) {
        Integer wareId = inventoryTransact.getWareId();
        String cellCode = inventoryTransact.getFromCellCode();
        String boxCode = inventoryTransact.getFromBoxCode();
        Integer packDetailId = inventoryTransact.getFromPackDetailId();
        Double transRatio = inventoryTransact.getFromTransRatio();
        String itemCode = inventoryTransact.getItemCode();
        Integer batchId = inventoryTransact.getFromBatchId();
        Inventory inventory = null;
        if (batchId == null) {
            String batchName = inventoryTransact.getFromBatchName();
            inventory = inventoryService.getSameInventoryNew(wareId, cellCode, boxCode, packDetailId, itemCode, batchName, transRatio);
        } else {
            inventory = inventoryService.getSameInventory(wareId, cellCode, boxCode, packDetailId, itemCode, batchId, transRatio, inventoryTransact.getFromBatchName());
        }
        inventory.setQuantity(inventory.getQuantity() - inventoryTransact.getFromQuantity());
        inventory.setAllotQuantity(inventory.getAllotQuantity() - inventoryTransact.getFromQuantity());
        if(inventory.getAllotQuantity()<0){
            inventory.setAllotQuantity(0.0);
        }
        inventoryService.updateById(inventory);
        //库存为0 直接删除库存
        if (inventory.getQuantity() <= 0) {
            inventoryService.removeById(inventory.getInventoryId());
        }
        return inventory;
    }

    private Inventory handleAdjust(InventoryTransact inventoryTransact) {
        Integer wareId = inventoryTransact.getWareId();
        String cellCode = inventoryTransact.getFromCellCode();
        String boxCode = inventoryTransact.getFromBoxCode();
        Integer packDetailId = inventoryTransact.getFromPackDetailId();
        Double transRatio = inventoryTransact.getFromTransRatio();
        String itemCode = inventoryTransact.getItemCode();
        Integer batchId = inventoryTransact.getFromBatchId();
        Inventory inventory = inventoryService.getSameInventory(wareId, cellCode, boxCode, packDetailId, itemCode, batchId, transRatio, inventoryTransact.getFromBatchName());

        inventory.setQuantity(inventoryTransact.getToQuantity());
        inventoryService.updateById(inventory);
        //库存为0 直接删除库存
        if (inventory.getQuantity() == 0) {
            inventoryService.removeById(inventory.getInventoryId());
        }
        return inventory;
    }

    /**
     * 转移类型事务提交<p>
     * 有三种，没有任何区别
     * 1)上架;
     * 2)整箱转移;
     * 3)整箱拣货;
     *
     * @param inventoryTransact 库存事务
     * @return Inventory
     */
    private Inventory handleTransfer(InventoryTransact inventoryTransact) {
//        //来源库存
//        QueryWrapper<Inventory> invQueryWrapper = new QueryWrapper();
//        //仓库
//        invQueryWrapper.eq(ObjectUtil.isNotEmpty(inventoryTransact.getWareId()), "ware_id", inventoryTransact.getWareId());
//        //批号
//        invQueryWrapper.eq(ObjectUtil.isNotEmpty(inventoryTransact.getFromBatchName()), "batch_name", inventoryTransact.getFromBatchName());
//        //物料编码
//        invQueryWrapper.eq(ObjectUtil.isNotEmpty(inventoryTransact.getItemCode()), "item_code", inventoryTransact.getItemCode());
//        //目标库位
//        invQueryWrapper.eq(ObjectUtil.isNotEmpty(inventoryTransact.getToCellCode()), "cell_code", inventoryTransact.getToCellCode());
//        //入库单号
//        invQueryWrapper.eq(ObjectUtil.isNotEmpty(inventoryTransact.getBillNo()), "asn_bill_no", inventoryTransact.getBillNo());
//        //入库明细单号
//        invQueryWrapper.eq(ObjectUtil.isNotEmpty(inventoryTransact.getDetailNo()), "asn_detail_no", inventoryTransact.getDetailNo());
//
//        Inventory fromInventory = inventoryService.getOne(invQueryWrapper);
        //过滤VMi直发虚仓物料
        List<Integer> list = new ArrayList<>();
        list.add(directId);
        list.add(vmiId);
        Inventory fromInventory = inventoryService.getById(inventoryTransact.getToInventoryId());
        fromInventory.setBoxCode(inventoryTransact.getToBoxCode());
        //只需要修改库位即可
        if (ObjectUtil.isNotNull(fromInventory)) {
            Integer integer = cellInfoService.findbyCellCode(inventoryTransact.getToCellCode());
            //todo erp导入过来有库存id,手动入库需要统一
            if (ObjectUtil.isNotEmpty(integer)) {
                fromInventory.setErpAreaId(integer);
            }
            fromInventory.setCellCode(inventoryTransact.getToCellCode());
            fromInventory.setTransRatio(1.0);
            if (list.contains(fromInventory.getAsnStockId())) {
                //如果是vmi，直发虚仓。冻结库存
                fromInventory.setFreezeQuantity(fromInventory.getFreezeQuantity() + fromInventory.getQuantity());
            }
            inventoryService.updateById(fromInventory);
        } else {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "未找到对应库存");
        }

//
//        //来源库存
//        QueryWrapper<Inventory> toInvQueryWrapper = new QueryWrapper();
//        //仓库
//        toInvQueryWrapper.eq(ObjectUtil.isNotEmpty(inventoryTransact.getWareId()), "ware_id", inventoryTransact.getWareId());
//        //批号
//        toInvQueryWrapper.eq(ObjectUtil.isNotEmpty(inventoryTransact.getFromBatchName()), "batch_name", inventoryTransact.getFromBatchName());
//        //物料编码
//        toInvQueryWrapper.eq(ObjectUtil.isNotEmpty(inventoryTransact.getItemCode()), "item_code", inventoryTransact.getItemCode());
//        //目标库位
//        toInvQueryWrapper.eq(ObjectUtil.isNotEmpty(inventoryTransact.getToCellCode()), "cell_code", inventoryTransact.getToCellCode());
//
//        Inventory toInv = inventoryService.getOne(invQueryWrapper);



//        //目的库存
//        Inventory toInventory = inventoryService.getSameInventory(
//                inventoryTransact.getWareId(),
//                inventoryTransact.getToCellCode(), //目标库位
//                inventoryTransact.getFromBoxCode(),//来源容器
//                inventoryTransact.getToPackDetailId(),
//                inventoryTransact.getItemCode(),
//                inventoryTransact.getToBatchId(),
//                inventoryTransact.getToTransRatio(),
//                inventoryTransact.getFromBatchName());
//        if (toInventory == null) {
//            toInventory = processInventoryData(inventoryTransact);
//            toInventory.setPackDescribe(fromInventory.getPackDescribe());
//            inventoryService.save(toInventory);
//        } else {
//            toInventory.setQuantity(toInventory.getQuantity() + inventoryTransact.getToQuantity());
//            inventoryService.updateById(toInventory);
//        }
//        if (true) {throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR);}
        return fromInventory;
    }

    /**
     * 组托类型事务提交<p>
     * 1)装箱 库位库存转移到容器库存 [库位库存扣减,容器库存新增]
     *
     * @param inventoryTransact 库存事务
     * @return Inventory
     */
    private Inventory handlePack(InventoryTransact inventoryTransact) {




        Inventory cellInventory = inventoryService.getById(inventoryTransact.getToInventoryId());
//        if (ObjectUtil.isEmpty(cellInventory)) {
//            cellInventory =   processInventoryData(inventoryTransact);
//        }
        if (ObjectUtil.isNull(cellInventory)) {
            cellInventory = processInventoryData(inventoryTransact);
            cellInventory.setQuantity(inventoryTransact.getToQuantity());
            cellInventory.setFreezeQuantity(inventoryTransact.getToQuantity());
            inventoryService.save(cellInventory);
         }


        String batchName = cellInventory.getBatchName();
        if (ObjectUtil.isNotEmpty(inventoryTransact.getAgreementNo()) && inventoryTransact.getAsnType() != 41) {
             batchName = inventoryTransact.getAgreementNo();
        }


        cellInventory.setCategoryId(inventoryTransact.getCategoryId());



        //扣减库位库存
        cellInventory.setQuantity(cellInventory.getQuantity() - inventoryTransact.getToQuantity());
        if (InventoryConstant.TRANSACT_STATE_UNNORMAL.equals(inventoryTransact.getState())) {
            cellInventory.setFreezeQuantity(cellInventory.getFreezeQuantity() - inventoryTransact.getToQuantity());
        }
        inventoryService.updateById(cellInventory);
        if (cellInventory.getQuantity() <= 0) {
            inventoryService.removeById(cellInventory.getInventoryId());
        }
        //新增明细行
        Inventory boxInventory;
        boxInventory = processInventoryData(inventoryTransact);
        boxInventory.setAgreementNo(inventoryTransact.getAgreementNo());
        boxInventory.setBatchName(batchName);
        boxInventory.setState(cellInventory.getState());
        boxInventory.setStateDis(cellInventory.getStateDis());
        boxInventory.setPackDescribe(cellInventory.getPackDescribe());
        inventoryService.save(boxInventory);
        return boxInventory;
    }

    /**
     * 入库类型事务提交<p>
     * 1)直接入库到库位
     * 2)直接入库到容器
     *
     * @param inventoryTransact 库存事务
     * @return 库存
     */
    private Inventory handleInStore(InventoryTransact inventoryTransact) {

             Inventory inventory;
             inventory  = processInventoryData(inventoryTransact);

             inventoryService.save(inventory);

        return inventory;
    }


    /**
     * 整理库存数据
     *
     * @param inventoryTransact 库存事务
     */
    public Inventory processInventoryData(InventoryTransact inventoryTransact) {
        Inventory inventory = new Inventory();
        //事务类型
        Integer transactType = inventoryTransact.getTransactType();
        //仓库
        Integer fromWareId = inventoryTransact.getWareId();
        String fromWareName = inventoryTransact.getWareName();
        //TODO:调拨可能单独处理
//        Integer toWareId = inventoryTransact.getToWareId();
//        String toWareName = inventoryTransact.getToWareName();
        inventory.setAsnStockId(inventoryTransact.getAsnStockId());
        inventory.setAsnStockName(inventoryTransact.getAsnStockName());
        inventory.setWareId(fromWareId);
        inventory.setWareName(fromWareName);
        inventory.setAgreementNo(inventoryTransact.getAgreementNo());
        inventory.setAcceptRecordCode(inventoryTransact.getAcceptRecordCode());
        //货主
        if (ObjectUtil.isNull(inventoryTransact.getToOrganizationId())) {
            //获取本仓库
            Organization sys_mine = organizationService.getOneByField("organization_code", "SYS_MINE");
            inventory.setOrganizationId(sys_mine.getOrganizationId());
            inventory.setOrganizationName(sys_mine.getOrganizationName());
        } else {
            inventory.setOrganizationId(inventoryTransact.getToOrganizationId());
            inventory.setOrganizationName(inventoryTransact.getToOrganizationName());
        }

        //库位
        String fromCellCode = inventoryTransact.getFromCellCode();
        String toCellCode = inventoryTransact.getToCellCode();
        if (transactType.equals(InventoryConstant.TRANSACT_TYPE_ACCEPT)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_BLIND)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_ASN)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_UP)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_MOVE)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_PICK)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_PICK2)) {
            //收存
            inventory.setCellCode(toCellCode);
            // 新增将erp仓库id传入库存--张宇
            Integer integer = cellInfoService.findbyCellCode(toCellCode);
            //todo erp导入过来有库存id,手动入库需要统一
            if (ObjectUtil.isNotEmpty(integer)) {
                inventory.setErpAreaId(integer);
            }
        } else {
            //发
            inventory.setCellCode(fromCellCode);
        }
        //箱号
        String fromBoxCode = inventoryTransact.getFromBoxCode();
        String toBoxCode = inventoryTransact.getToBoxCode();
        if (transactType.equals(InventoryConstant.TRANSACT_TYPE_ACCEPT)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_BLIND)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_ASN)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_PACK)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_PICK2)
        ) {
            //收存
            inventory.setBoxCode(toBoxCode);
        } else {
            //发
            inventory.setBoxCode(fromBoxCode);
        }
        //物料
        inventory.setItemCode(inventoryTransact.getItemCode());
        inventory.setItemName(inventoryTransact.getItemName());
        inventory.setSpec(inventoryTransact.getSpec());
        inventory.setModel(inventoryTransact.getModel());
        inventory.setImgUrl(inventoryTransact.getImgUrl());
        //批次
//        Integer fromBatchId = inventoryTransact.getFromBatchId();
//        String fromBatchName = inventoryTransact.getFromBatchName();  批次应该只有一种情况
        Integer toBatchId = inventoryTransact.getToBatchId();
        String toBatchName = inventoryTransact.getToBatchName();
        inventory.setBatchId(toBatchId);
        inventory.setBatchName(toBatchName);

        //数量
        Double fromQuantity = inventoryTransact.getFromQuantity();
        Double toQuantity = inventoryTransact.getToQuantity();
        if (transactType.equals(InventoryConstant.TRANSACT_TYPE_ACCEPT)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_BLIND)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_ASN)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_PACK)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_PICK2)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_UP)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_MOVE)) {
            inventory.setQuantity(toQuantity);
        } else {
            inventory.setQuantity(fromQuantity);
        }
        //计量单位
        Integer fromPackDetailId = inventoryTransact.getFromPackDetailId();
        String fromPackDescribe = inventoryTransact.getFromPackDescribe();
        Double fromTransRatio = inventoryTransact.getFromTransRatio();
        Integer toPackDetailId = inventoryTransact.getToPackDetailId();
        String toPackDescribe = inventoryTransact.getToPackDescribe();
        Double toTransRatio = inventoryTransact.getToTransRatio() == null ? 1.00 : inventoryTransact.getToTransRatio();
        if (transactType.equals(InventoryConstant.TRANSACT_TYPE_ACCEPT)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_BLIND)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_ASN)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_PICK2)
                || transactType.equals(InventoryConstant.TRANSACT_TYPE_UP)) {
            inventory.setPackDetailId(toPackDetailId);
            inventory.setPackDescribe(toPackDescribe);
            inventory.setTransRatio(toTransRatio);
        } else {
            inventory.setPackDetailId(fromPackDetailId);
            inventory.setPackDescribe(fromPackDescribe);
            inventory.setTransRatio(fromTransRatio);
        }
        //状态 0-冻结 1-正常
        inventory.setState(inventoryTransact.getState());
        if (ObjectUtil.isNotNull(inventoryTransact.getState()) && InventoryConstant.TRANSACT_STATE_UNNORMAL.equals(inventoryTransact.getState())) {
            inventory.setFreezeQuantity(inventory.getQuantity());
        }
        inventory.setStateDis(inventoryTransact.getStateDis());


        //新增任务单号，明细行
        inventory.setAsnDetailNo(inventoryTransact.getDetailNo());
        inventory.setAsnBillNo(inventoryTransact.getBillNo());
        inventory.setMtoNo(inventoryTransact.getMtoNo());
        inventory.setCategoryId(inventoryTransact.getCategoryId());
        //单据号 delete
//        inventory.setBillNo(inventoryTransact.getBillNo());
        return inventory;
    }

    /**
     * 这个接口用于update或者save inventory
     *
     * @param fromInventory  update 修改之前的   save null
     * @param toInventory
     * @param transactType
     * @param detailNo
     * @param createUserId
     * @param createUserName
     * @return
     */
    @Override
    public Map run(Inventory fromInventory, Inventory toInventory, Integer transactType, String detailNo, Integer createUserId, String createUserName, Integer relateId) {
        InventoryTransact inventoryTransact = new InventoryTransact();
        inventoryTransact.setCreateTime(DateUtils.getNowDateTimeString());
        inventoryTransact.setCreateUserId(createUserId);
        inventoryTransact.setCreateUserName(createUserName);
        inventoryTransact.setWareId(fromInventory.getWareId());
        inventoryTransact.setWareName(fromInventory.getWareName());
        inventoryTransact.setItemCode(fromInventory.getItemCode());
        inventoryTransact.setItemName(fromInventory.getItemName());
        inventoryTransact.setFromCellCode(fromInventory.getCellCode());
        inventoryTransact.setFromBoxCode(fromInventory.getBoxCode());
        inventoryTransact.setTransactType(transactType);
        inventoryTransact.setState(fromInventory.getState());
        inventoryTransact.setFromOrganizationId(fromInventory.getOrganizationId());
        inventoryTransact.setFromOrganizationName(fromInventory.getOrganizationName());
        inventoryTransact.setFromBatchId(fromInventory.getBatchId());
        inventoryTransact.setFromBatchName(fromInventory.getBatchName());
        inventoryTransact.setFromBoxCode(fromInventory.getBoxCode());
        inventoryTransact.setFromPackDetailId(fromInventory.getPackDetailId());
        inventoryTransact.setFromPackDescribe(fromInventory.getPackDescribe());
        inventoryTransact.setDetailNo(detailNo);
        inventoryTransact.setFromQuantity(fromInventory.getQuantity());
        inventoryTransact.setRelateId(relateId);


        inventoryTransact.setToBatchId(toInventory.getBatchId());
        inventoryTransact.setToBatchName(toInventory.getBatchName());
        inventoryTransact.setToBoxCode(toInventory.getBoxCode());
        inventoryTransact.setToCellCode(toInventory.getCellCode());
        inventoryTransact.setToOrganizationId(toInventory.getOrganizationId());
        inventoryTransact.setToOrganizationName(toInventory.getOrganizationName());
        inventoryTransact.setToPackDescribe(toInventory.getPackDescribe());
        inventoryTransact.setToPackDetailId(toInventory.getPackDetailId());
        inventoryTransact.setToQuantity(toInventory.getQuantity());
        inventoryTransact.setToTransRatio(toInventory.getTransRatio());

        if (ObjectUtil.isEmpty(toInventory)) {
            inventoryTransact.setFromBatchId(fromInventory.getBatchId());
            inventoryTransact.setFromBatchName(fromInventory.getBatchName());
            inventoryTransact.setFromBoxCode(fromInventory.getBoxCode());
            inventoryTransact.setFromOrganizationId(fromInventory.getOrganizationId());
            inventoryTransact.setFromOrganizationName(fromInventory.getOrganizationName());
            inventoryTransact.setFromPackDescribe(fromInventory.getPackDescribe());
            inventoryTransact.setFromPackDetailId(fromInventory.getPackDetailId());
            inventoryTransact.setFromQuantity(fromInventory.getQuantity());
            inventoryTransact.setFromTransRatio(fromInventory.getTransRatio());
            inventoryTransact.setStateDis(toInventory.getStateDis());
            inventoryService.save(toInventory);
        } else {
            inventoryService.updateById(toInventory);
        }

        super.save(inventoryTransact);

        Map map = new HashMap();
        map.put("inventory", fromInventory);
        map.put("toInventory", toInventory);
        map.put("inventoryTransact", inventoryTransact);

        return map;
    }

    /**
     * 分页总数
     *
     * @param criteria
     * @return
     */
    @Override
    public long findCount(InventoryTransactCriteria criteria) {
      return   baseMapper.findCount(criteria);
    }


}
